﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Hkwl.WorldExpo.Merchant.Common;
using Hkwl.WorldExpo.Merchant.Model;
using Hkwl.WorldExpo.Merchant.Service;
using System.Collections.Generic;

namespace Hkwl.WorldExpo.Merchant.Web
{
    public partial class ClientStatistics : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataBind_ClientStatistics(0);
            }
        }

        protected void DataBind_ClientStatistics(int recordcount)
        {
            if (Session["MerchantId"] != null)
            {
                int merchantId = int.Parse(Session["MerchantId"].ToString());
                if (recordcount == 0)
                {
                    this.AspNetPager_Content.CurrentPageIndex = 1;
                }
                int count = recordcount;
                int pageIndex = this.AspNetPager_Content.CurrentPageIndex;
                int pageSize = this.AspNetPager_Content.PageSize;

                this.GridView_Client.DataSource = ServiceLocator.ILogService.GetClientReporting(merchantId, pageIndex, pageSize, ref count);
                this.GridView_Client.DataBind();
                this.AspNetPager_Content.RecordCount = count;
            }

        }

        protected void GridView_Client_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                IList client = (IList)e.Row.DataItem;

                Label lbNum = (Label)e.Row.FindControl("lbNum");
                lbNum.Text = (e.Row.RowIndex + 1).ToString();

                Label lbClientName = (Label)e.Row.FindControl("lbClientName");
                lbClientName.Text = Convert.ToString(client[1]);

                Label lbCompany = (Label)e.Row.FindControl("lbCompany");
                lbCompany.Text = Convert.ToString(client[2]).Length > 8 ? Convert.ToString(client[2]).Substring(0, 8) + ".." : Convert.ToString(client[2]);

                Label lbCellPhone = (Label)e.Row.FindControl("lbCellPhone");
                lbCellPhone.Text = Convert.ToString(client[3]);

                Label lbProjectNum = (Label)e.Row.FindControl("lbProjectNum");
                lbProjectNum.Text = Convert.ToString(client[4]);

                Label lbUseNum = (Label)e.Row.FindControl("lbUseNum");
                lbUseNum.Text = Convert.ToString(client[5]);

                Label lbRate = (Label)e.Row.FindControl("lbRate");
                lbRate.Text = Math.Round(Convert.ToDecimal(client[6])*100,2).ToString()+"%";

                HyperLink hlViewProjectLog = (HyperLink)e.Row.FindControl("hlViewProjectLog");
                hlViewProjectLog.NavigateUrl = "ClientReceiveDetail.aspx?ClientId="+ Convert.ToString(client[0]);

                HyperLink hlViewUseNUmLog = (HyperLink)e.Row.FindControl("hlViewUseNUmLog");
                hlViewUseNUmLog.NavigateUrl = "ClientUseDetail.aspx?ClientId=" + Convert.ToString(client[0]);

                if (lbProjectNum.Text == "0")
                {
                    hlViewProjectLog.Visible = false;
                }
                if (lbUseNum.Text == "0")
                {
                    hlViewUseNUmLog.Visible = false;
                }
            }
        }

        protected void AspNetPager_Content_PageChanged(object sender, EventArgs e)
        {
            DataBind_ClientStatistics(this.AspNetPager_Content.RecordCount);
        }
    }
}
